import csv

from flask import Flask, render_template
from itsdangerous import json

app = Flask(__name__)

# 路由解析，通过用户访问的路径，匹配相应的函数
@app.route("/")
def hello():
    return render_template('demo.html') #开启

# 1-12月各月销售利润情况
@app.route('/sales_month')
def sales_month():
    data = []
    # TODO 读取data/sales_month.csv文件中的数据
    with open("data/sales_month.csv","r",encoding="utf8") as f:
        reader = csv.reader(f)
        next(reader)
        print(type(reader))

        for row in reader:
           data.append({"month":row[0],"sales":row[1],"profit":row[2]})
    return {'data':data}

# 各地区销售和利润情况
@app.route('/sales_region')
def sales_region():
    data = []
    # TODO 读取data/sales_region.csv文件中的数据
    with open("data/sales_region.csv","r",encoding="utf8") as f:
        reader = csv.reader(f)
        next(reader)
        print(type(reader))
        for row in reader:
           data.append({"region":row[0],"sales":row[1],"profit":row[2]})
    return {'data':data}

# 各产品销售情况
@app.route('/sales_product')
def sales_product():
    # TODO 读取data/sales_product.csv文件中的数据
    with open("data/sales_product.json","r",encoding="utf8") as f:
        r_json = json.load(f)
    return {'data':r_json }


# 各省份销售利润情况
@app.route('/sales_province')
def sales_province():
    data = []
    # TODO 读取data/sales_province.csv文件中的数据
    with open("data/sales_province.csv","r",encoding="utf8") as f:
        reader = csv.reader(f)
        next(reader)
        print(type(reader))
        for row in reader:
           data.append({"province":row[0],"sales":row[1],"profit":row[2]})
    return {'data':data}

# 各地区产品经理和利润情况
@app.route('/sales_manager')
def sales_manager():
    data = []
    # TODO 读取data/sales_manager.csv文件中的数据
    with open("data/sales_manager.csv","r",encoding="utf8") as f:
        reader = csv.reader(f)
        next(reader)
        print(type(reader))
        for row in reader:
           data.append({"sales_manager":row[0],"sales":row[1],"profit":row[2]})
    return {'data':data}

if __name__ == '__main__':
    app.run()
